package com.hust360.percent.pojo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.pojo.Shop.ShopDAO;

@SuppressLint("SimpleDateFormat")
public class ShopDAOImpl implements ShopDAO {

	SQLiteDatabase db;
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	public ShopDAOImpl(Application application) {
		db = ((PercentApplication) application).getLocalDB();
	}

	/**
	 * 向数据库中插入数据的方法
	 */
	@Override
	public void save(Shop shop) {
		ContentValues values = new ContentValues();
		values.put(Shop.ADDRESS, shop.address);
		values.put(Shop.AREA_ID, shop.areaid);
		values.put(Shop.BRAND_ID, shop.brandid);
		values.put(Shop.CITY_ID, shop.cityid);
		values.put(Shop.ID, shop.id);
		values.put(Shop.LATITUDE, shop.latitude);
		values.put(Shop.LOGO_IMG, shop.logoimg);
		values.put(Shop.LOGO_URL, shop.logoURL);
		values.put(Shop.LONGITUDE, shop.longitude);

		try {
			String modstr = format.format(shop.moddate);
			values.put(Shop.MODIFY_DATE, modstr);
		} catch (Exception e) {
		}

		values.put(Shop.NAME, shop.name);
		values.put(Shop.SUBNAME, shop.subname);
		values.put(Shop.TELEPHONE, shop.telephone);
		values.put(Shop.TYPE, shop.type);

		synchronized (PercentApplication.localDB) {
			PercentApplication.localDB.insertWithOnConflict(Shop.TABLE_NAME,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
		}
	}

	@Override
	public long deleteById(Long id) {
		long l;
		synchronized (db) {
			l = db.delete(Shop.TABLE_NAME, "id=?", new String[] { id + "" });
		}
		return l;
	}

	public List<Shop> findByArgs(String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {

		List<Shop> list;

		Cursor c;

		synchronized (db) {
			c = db.query(Shop.TABLE_NAME, columns, selection, selectionArgs,
					groupBy, having, orderBy);
		}
		list = new ArrayList<Shop>(c.getCount());
		Shop shop;
		while (c.moveToNext()) {
			shop = new ShopLocal();
			shop.address = c.getString(c.getColumnIndex(Shop.ADDRESS));
			shop.areaid = c.getLong(c.getColumnIndex(Shop.AREA_ID));
			shop.brandid = c.getLong(c.getColumnIndex(Shop.BRAND_ID));
			shop.cityid = c.getLong(c.getColumnIndex(Shop.CITY_ID));
			shop.id = c.getLong(c.getColumnIndex(Shop.ID));
			shop.latitude = c.getDouble(c.getColumnIndex(Shop.LATITUDE));
			shop.logoimg = c.getString(c.getColumnIndex(Shop.LOGO_IMG));
			shop.logoURL = c.getString(c.getColumnIndex(Shop.LOGO_URL));
			shop.longitude = c.getDouble(c.getColumnIndex(Shop.LONGITUDE));

			try {
				shop.moddate = format.parse(c.getString(c
						.getColumnIndex(Shop.MODIFY_DATE)));
			} catch (Exception e) {
				e.printStackTrace();
				shop.moddate = null;
			}

			shop.name = c.getString(c.getColumnIndex(Shop.NAME));
			shop.subname = c.getString(c.getColumnIndex(Shop.SUBNAME));
			shop.telephone = c.getString(c.getColumnIndex(Shop.TELEPHONE));
			shop.type = c.getInt(c.getColumnIndex(Shop.TYPE));
			list.add(shop);
		}
		c.close();
		return list;
	}

	@Override
	public Shop findById(Long id) {
		Shop shop = null;
		Cursor c;
		synchronized (PercentApplication.localDB) {
			c = PercentApplication.localDB.rawQuery(
					"SELECT * FROM shop WHERE id=?", new String[] { id + "" });
		}

		if (c.moveToFirst()) {
			shop = new ShopLocal();
			shop.address = c.getString(c.getColumnIndex(Shop.ADDRESS));
			shop.areaid = c.getLong(c.getColumnIndex(Shop.AREA_ID));
			shop.brandid = c.getLong(c.getColumnIndex(Shop.BRAND_ID));
			shop.cityid = c.getLong(c.getColumnIndex(Shop.CITY_ID));
			shop.id = c.getLong(c.getColumnIndex(Shop.ID));
			shop.latitude = c.getDouble(c.getColumnIndex(Shop.LATITUDE));
			shop.logoimg = c.getString(c.getColumnIndex(Shop.LOGO_IMG));
			shop.logoURL = c.getString(c.getColumnIndex(Shop.LOGO_URL));
			shop.longitude = c.getDouble(c.getColumnIndex(Shop.LONGITUDE));

			try {
				shop.moddate = format.parse(c.getString(c
						.getColumnIndex(Shop.MODIFY_DATE)));
			} catch (Exception e) {
				e.printStackTrace();
				shop.moddate = null;
			}

			shop.name = c.getString(c.getColumnIndex(Shop.NAME));
			shop.subname = c.getString(c.getColumnIndex(Shop.SUBNAME));
			shop.telephone = c.getString(c.getColumnIndex(Shop.TELEPHONE));
			shop.type = c.getInt(c.getColumnIndex(Shop.TYPE));
		}
		return shop;
	}

	@Override
	public List<Shop> findWithMap(Map<String, String> map) {
		Set<String> set = map.keySet();
		List<String> columns = Arrays.asList(Shop.columns);
		String selection = "";
		Cursor c;
		List<Shop> list;
		Shop shop;

		for (String key : set) {
			String name = (String) map.get(key);
			if (columns.contains(key) && !"".equals(name)) {
				selection += key + "='" + name + "' AND ";
			}
		}
		selection += "'1'='1'";
		synchronized (db) {
			c = db.rawQuery("SELECT * FROM shop WHERE " + selection, null);
		}

		list = new ArrayList<Shop>(c.getCount());

		while (c.moveToNext()) {
			shop = new ShopLocal();
			shop.address = c.getString(c.getColumnIndex(Shop.ADDRESS));
			shop.areaid = c.getLong(c.getColumnIndex(Shop.AREA_ID));
			shop.brandid = c.getLong(c.getColumnIndex(Shop.BRAND_ID));
			shop.cityid = c.getLong(c.getColumnIndex(Shop.CITY_ID));
			shop.id = c.getLong(c.getColumnIndex(Shop.ID));
			shop.latitude = c.getDouble(c.getColumnIndex(Shop.LATITUDE));
			shop.logoimg = c.getString(c.getColumnIndex(Shop.LOGO_IMG));
			shop.logoURL = c.getString(c.getColumnIndex(Shop.LOGO_URL));
			shop.longitude = c.getDouble(c.getColumnIndex(Shop.LONGITUDE));

			try {
				shop.moddate = format.parse(c.getString(c
						.getColumnIndex(Shop.MODIFY_DATE)));
			} catch (Exception e) {
				e.printStackTrace();
				shop.moddate = null;
			}

			shop.name = c.getString(c.getColumnIndex(Shop.NAME));
			shop.subname = c.getString(c.getColumnIndex(Shop.SUBNAME));
			shop.telephone = c.getString(c.getColumnIndex(Shop.TELEPHONE));
			shop.type = c.getInt(c.getColumnIndex(Shop.TYPE));
			list.add(shop);
		}
		c.close();
		return list;
	}

	void execSQL(String sql) {
		synchronized (db) {
			db.execSQL(sql);
		}
	}

	@Override
	public List<Shop> findByAreaId(Long shopid) {
		List<Shop> list;
		Shop shop;
		Cursor c;
		Date moddate = null;
		
		c = PercentApplication.localDB.rawQuery("SELECT * FROM shop WHERE "
				+ Shop.AREA_ID + "='" + shopid + "'", null);
		list = new ArrayList<Shop>(c.getCount());
		while (c.moveToNext()) {
			shop = new ShopLocal();
			shop.address = c.getString(c.getColumnIndex(Shop.ADDRESS));
			shop.areaid = c.getLong(c.getColumnIndex(Shop.AREA_ID));
			shop.brandid = c.getLong(c.getColumnIndex(Shop.BRAND_ID));
			shop.cityid = c.getLong(c.getColumnIndex(Shop.CITY_ID));
			shop.id = c.getLong(c.getColumnIndex(Shop.ID));
			shop.latitude = c.getDouble(c.getColumnIndex(Shop.LATITUDE));
			shop.longitude = c.getDouble(c.getColumnIndex(Shop.LONGITUDE));
			
			try {
				moddate = format.parse(c.getString(c.getColumnIndex(Shop.MODIFY_DATE)));
				shop.moddate = moddate;
			} catch (Exception e) {
				shop.moddate = null;
			}
			shop.name = c.getString(c.getColumnIndex(Shop.NAME));
			shop.subname = c.getString(c.getColumnIndex(Shop.SUBNAME));
			shop.telephone = c.getString(c.getColumnIndex(Shop.TELEPHONE));
			shop.type = c.getInt(c.getColumnIndex(Shop.TYPE));
			list.add(shop);
		}
		c.close();
		return list;
	}

}
